﻿using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

[Table("chapters")]
public class Chapter
{
    // 章节ID
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ChapterId { get; set; }

    // 书籍ID
    public int BookId { get; set; }

    // 章节名称
    [Required]
    [StringLength(255)]
    public string ChapterName { get; set; }

    // 章节序号
    public int ChapterNumber { get; set; }

    // 上传时间
    public DateTime UploadTime { get; set; }

    // 章节内容
    [Required]
    [StringLength(65535)] // 限制最多 30,000 个字符
    public string Content { get; set; }

    // 导航属性，用于与Book实体进行关联
    public Book Book { get; set; }

    // 静态方法用于配置实体
    public static void Configure(EntityTypeBuilder<Chapter> builder)
    {
        builder.HasKey(c => c.ChapterId);
        builder.Property(c => c.ChapterId).ValueGeneratedOnAdd();
        builder.Property(c => c.Content).IsRequired().HasMaxLength(30000); // 设置章节内容字段为必填且最大长度为 30,000

        // 定义与Book实体的关系
        builder.HasOne(c => c.Book)
            .WithMany(b => b.Chapters) // 假设Book实体有一个名为Chapters的集合导航属性
            .HasForeignKey(c => c.BookId)
            .OnDelete(DeleteBehavior.Restrict); // 防止级联删除
    }
}

public class ChapterContent
{
    public string Title { get; set; }
    public string Content { get; set; }
}